<template>
  <div>
    <el-dialog
      :title="form.id ? '商品编辑' : '商品添加'"
      :visible.sync="buffer.Bufferfrom"
      @opened="opend"
    >
      <el-form :model="form">
        <!-- 商品 -->
        <el-form-item label="一级分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择活动区域"
            @change="changeFirst"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" :label-width="formLabelWidth">
          <el-select v-model="form.second_cateid" placeholder="请选择">
            <el-option
              v-for="item in secondCate"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 名称 -->
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="form.goodsname" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 图片 -->
        <el-form-item label="商品图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="imgchange"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 规格 -->
        <el-form-item label="商品规格名" :label-width="formLabelWidth">
          <el-select
            v-model="form.specsid"
            placeholder="请选择商品规格名"
            @change="changeSpecs"
          >
            <el-option
              v-for="item in specsList"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品规格值" :label-width="formLabelWidth">
          <el-select v-model="form.specsattr" multiple placeholder="请选择">
            <el-option
              v-for="item in specsAttrs"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 销售价 -->
        <el-form-item label="销售价" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 市场价 -->
        <el-form-item label="市场价" :label-width="formLabelWidth">
          <el-input v-model="form.market_price" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 商品描述 -->
        <el-form-item label="商品详情描述" :label-width="formLabelWidth">
          <div ref="editor"></div>
        </el-form-item>
        <!-- 是否新品 -->
        <el-form-item label="是否新品" :label-width="formLabelWidth">
          <el-radio v-model="form.isnew" :label="1">是</el-radio>
          <el-radio v-model="form.isnew" :label="2">否</el-radio>
        </el-form-item>
        <!-- 是否热卖 -->
        <el-form-item label="是否热卖" :label-width="formLabelWidth">
          <el-radio v-model="form.ishot" :label="1">是</el-radio>
          <el-radio v-model="form.ishot" :label="2">否</el-radio>
        </el-form-item>
        <!-- 是否上架 -->
        <el-form-item label="是否新品" :label-width="formLabelWidth">
          <el-radio v-model="form.status" :label="1">是</el-radio>
          <el-radio v-model="form.status" :label="2">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor";
export default {
  props: ["buffer", "mylist", "cateList", "specsList"],
  name: "",
  components: {},
  data() {
    return {
      form: {
        first_cateid: "", //一级分类数据id
        second_cateid: "", //二级分类数据id
        goodsname: "", //商品名称
        price: "", //商品价格
        market_price: "", //商品市场价格
        img: "", //商品图片
        description: "", //商品描述
        specsid: "", //商品规格id
        specsattr: [], //商品规格值
        isnew: 1, //是否新品
        ishot: 1, //是否热卖
        status: 1, //1 启用  2禁用
      },
      secondCate: [],
      specsAttrs: [],
      imageUrl: "",
      editor: null,
      formLabelWidth: "120px",
    };
  },
  created() {},
  watch: {
    buffer: {
      handler(newV, oldV) {
        if (newV.formBuffer == false) {
          this.form = {
            first_cateid: "", //一级分类数据id
            second_cateid: "", //二级分类数据id
            goodsname: "", //商品名称
            price: "", //商品价格
            market_price: "", //商品市场价格
            img: "", //商品图片（二进制数据）
            description: "", //商品描述
            specsid: "", //商品规格id
            specsattr: [], //商品规格值
            isnew: 1, //是否新品
            ishot: 1, //是否热卖
            status: 1, //1 启用  2禁用
          };
          this.imageUrl = "";
          this.secondCate = []; //二级分类数据
          this.specsAttrs = []; //商品规格值
          this.editor.destroy();
        }
      },
      deep: true, 
    },
  },
  mounted() {},
  methods: {
    imgchange(file) {
      let { name, size } = file;
      if (size > 1024 * 1024 * 2) {
        this.$message({
          message: "大小超过2M！",
          type: "warning",
        });
      }
      let allowSuffix = ["jpeg", "jpg", "png", "gif"];
      if (!allowSuffix.includes(name.split(".").pop())) {
        this.$message({
          message: "非法文件！",
          type: "warning",
        });
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
    opend() {
      //获取富文本插件实例对象
      this.editor = new E(this.$refs.editor);
      this.editor.create();
      if (this.form.id) {
        this.editor.txt.html(this.form.description);
         this.editor.destroy();
      }
    },
    changeFirst(id) {
      // 根据一级分类数据id查找自己的孩子数据children
      this.secondCate = this.cateList.find((item) => item.id == id).children;
    },
    changeSpecs(id) {
      this.specsAttrs = this.specsList.find((item) => item.id == id).attrs;
    },
    submit() {
      // 追加商品描述信息
      this.form.description = this.editor.txt.html();
      this.form.specsattr = this.form.specsattr.join(",");
      (this.form.id
        ? this.$api.goodsEdit(this.form)
        : this.$api.goodsAdd(this.form)
      )
        .then((res) => {
          console.log(res);
          this.buffer.Bufferfrom = false;
          this.$message({ type: "success", message: "操作成功！" });
          this.$emit("channel", "更新数据吧！");
        })
        .catch((err) => console.log(err));
    },
  },
};
</script>
<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
